package DAO;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import Beans.Agent;
import Beans.Branch;
import Utils.HibernateUtil;

public class BranchDao {
	private SessionFactory sessionFactory;

	public BranchDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	Transaction tr = null;

	/**
	 * 返回所有网点信息
	 */
	public List<Branch> getAllbranch() {
		Session session = sessionFactory.openSession();

		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Branch.class);
		c.add(Restrictions.eq("status", 0));
		List<Branch> branchs = c.list();
		tr.commit();

		session.close();
		return branchs;
	}
	
	/**
	 * 根据物业公司获取设备网点
	 * @param estateID
	 * @return
	 */
	public List<Branch> getBranchByEstateID(int estateID) {
		EstateDao estateDao = new EstateDao();
		Session session = sessionFactory.openSession();
		
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Branch.class);
		c.add(Restrictions.eq("estate", estateDao.getEstateById(estateID)));
		c.add(Restrictions.eq("status", 0));
		List<Branch> branchs = c.list();
		tr.commit();
		
		session.close();
		return branchs;
	}

	/**
	 * 根据代理商id返回网点集合
	 */
	public List<Branch> getBranchByAgentId(int agentId) {
		AgentDao agentDao = new AgentDao();
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Branch.class);
		c.add(Restrictions.eq("agent", agentDao.getAgentById(agentId)));
		c.add(Restrictions.eq("status", 0));
		List<Branch> branchs = c.list();
		tr.commit();
		session.close();
		return branchs;
	}
	/**
	 * 根据物业公司id返回网点集合
	 */
	public List<Branch> getBranchByEstateId(int estateId) {
		EstateDao estateDao = new EstateDao();
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Branch.class);
		c.add(Restrictions.eq("estate", estateDao.getEstateById(estateId)));
		c.add(Restrictions.eq("status", 0));
		List<Branch> branchs = c.list();
		tr.commit();
		session.close();
		return branchs;
	}

	/**
	 * 通过网点ID返回指定网点信息
	 * 
	 * @param branchId
	 *            网点ID
	 * @return
	 */
	public Branch getBranchById(int branchId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Branch branch = (Branch) session.get(Branch.class, branchId);
		tr.commit();
		session.close();
		return branch;
	}

	/**
	 * 删除网点
	 */
	public void delete(Branch branch) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(branch);
		tr.commit();
		session.close();
	}

	/**
	 * 修改网点
	 */
	public void edit(Branch branch) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(branch);
		tr.commit();
		session.close();
	}

	/**
	 * 获取所有网点的列表
	 */
	public List<Branch> getAll() {
		Session session = sessionFactory.openSession();

		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Branch.class);
		c.add(Restrictions.eq("status", 0));
		List<Branch> branchs = c.list();
		tr.commit();

		session.close();
		return branchs;
	}

	/**
	 * 获取所有删选网点的列表
	 */
	public List<Branch> getAllSearchBranch(String branchProvince,String branchCity,int agentID,String branchName) {
		AgentDao agentDao = new AgentDao();
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(Branch.class);
		c.add(Restrictions.eq("status", 0));
		if (!"请选择省份".equals(branchProvince)) {
			c.add(Restrictions.eq("branchProvince", branchProvince));
			if(!"".equals(branchCity)){
				c.add(Restrictions.eq("branchCity", branchCity));
			}
		}else if(agentID!=0){
			c.add(Restrictions.eq("agent", agentDao.getAgentById(agentID)));
		} 
		else if (!"".equals(branchName)) {
			c.add(Restrictions.like("branchName", "%"+branchName+"%"));
		} else {

		}
		List<Branch> branchs = c.list();
		tr.commit();
		session.close();
		return branchs;
	}

	/**
	 * 保存新增的网点
	 */
	public Branch save(Branch branch) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.save(branch);
		tr.commit();
		session.close();
		return branch;
	}
}
